package com.system.healthproject.model.doctor.service.imp;

import com.system.healthproject.bean.DoctorAddPatientActivity;
import com.system.healthproject.bean.DoctorOnePatientActivityBean;
import com.system.healthproject.bean.DoctorPatientActivityBean;

import com.system.healthproject.mapper.*;
import com.system.healthproject.model.doctor.service.DoctorPatientActivityService;

import com.system.healthproject.pojo.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Xuheng Zhou
 * @date 2020/6/11 10:38
 * @desc
 */
@Service
@Transactional
public class DoctorPatientActivityServiceImpl implements DoctorPatientActivityService {

    @Autowired
    private PatientActivityMapper mapper;

    @Autowired
    private PatientActivitySignUpMapper signUpMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private TransactionMapper transactionMapper;

    @Override
    public List<DoctorPatientActivityBean> selectDoctorPatientActivity(Integer doctorId, Integer page, Integer size) {


        return mapper.selectDoctorPatientActivity(doctorId, (page - 1) * size, size);
    }

    @Override
    public DoctorOnePatientActivityBean selectDoctorOnePatientActivity(Integer patientactivityId) {

        //查询该活动报名的人数
        Integer signUpPersonNum = signUpMapper.selectSignUpPerson(patientactivityId);
        //获取除报名人数以外的所有信息
        DoctorOnePatientActivityBean doctorOnePatientActivityBean = mapper.selectDoctorOnePatientActivity(patientactivityId);
        //把查询到的报名人数放入封装的bean中的报名人数中返回
        doctorOnePatientActivityBean.setSignUpPersonNumber(signUpPersonNum);

        return doctorOnePatientActivityBean;
    }

    @Override
    public boolean insertPatientActivity(DoctorAddPatientActivity addPatientActivity) {
        //设置患教创建时间
        addPatientActivity.setPatientActivityCreateTime(new Date());

        return mapper.doctorAddPatientActivity(addPatientActivity) > 0 ? true : false;
    }

    @Override
    public boolean cancelPatientActivity(Integer patientactivityId, String cancelreason) {
        return mapper.cancelPatientActivity(patientactivityId, cancelreason, new Date()) > 0 ? true : false;
    }

    @Override
    public boolean doctorClosePatientActivity(Integer patientactivityId, Integer loginId) {
        /**
         *   结束，把直播结束，添加结束时间，然后修改状态为已结束，同时再获取观看人数，
         *   并把活动核桃币平分给每个观看的患者，每个患者的核桃币均增加
         */

        boolean tf = true; //判断该活动的医生id和token的医生id是否相同
        boolean tf1 = false;//判断医生账户是否减少
        boolean tf2 = false;//判断医生的交易信息是否增加
        boolean tf3 = false;//判断患者的账户是否增加
        boolean tf4 = false;//判断患者的交易信息是否增加
        //通过token中的医生的loginId获取医生id
        Integer doctorId = selectDoctorIdByLoginId(loginId);
        //查询患教活动中的对应的主持人（医生id）
        Integer PaDoctorId = mapper.selectPAdoctorIdByPAid(patientactivityId);

        //如果前台传来的医生id和对应的患教活动的医生id相同，则是想要操作的患教活动
        //则前台传入的患教活动id并没有错
        if (doctorId.equals(PaDoctorId)) {

            //获取观看人数
            Integer watchPersonCount = signUpMapper.selectWatchPerson(patientactivityId);
            //获取患教直播的核桃币
            Double activityMoney = mapper.selectActivityMoney(patientactivityId);

            //获取观看患教直播的每一位患者的id
            List<Integer> list = signUpMapper.selectActivityWatchPatient(patientactivityId);


            //说明有人观看，就可以进行分钱和添加交易信息
            if(list.size()>0 && watchPersonCount>0) {
                //获取患者id对应的login的id
                List<Integer> loginIdList = loginMapper.selectLoginIdByPatientIdList(list);
                //每位观看的患者平分到的核桃币
                double money = (activityMoney) / ((double) watchPersonCount);

                //添加结束时间，并把状态更改为已结束
                tf = mapper.doctorClosePatientActivity(patientactivityId, new Date()) > 0 ? true : false;
                //说明患教活动的信息以及更改为已结束，然后计算想要的金额
                if (tf == true) {

                    //则医生的金额就对应减少
                    tf1 = accountMapper.updateDoctorMoneyByLoginId(loginId, activityMoney) > 0 ? true : false;

                    // 则开始增加患者账户余额后，再增加相应的交易信息
                    if (tf1 == true) {

                        //把平分的核桃币增加到观看直播的每一位患者的账户上
                        for (Integer PatientLoginId : loginIdList) {

                            System.out.println(PatientLoginId);


                            Transaction transaction = new Transaction();
                            //付款登录用户id
                            transaction.setLoginId(loginId);
                            //交易类型
                            transaction.setTransactionStatuetype(1);
                            //交易时间
                            transaction.setTransactionTime(new Date());
                            //交易核桃币数量
                            transaction.setTransactionVirtualmoney(activityMoney);
                            //收款用户id
                            transaction.setCollectionLoginid(PatientLoginId);
                            //交易场景
                            transaction.setTransactionType("创建患教活动支付");
                            //交易状态（1成功  2 失败 3待支付）
                            transaction.setTransactionState(1);


                            tf2 = transactionMapper.insertSelective(transaction) > 0 ? true : false;

                            //说明医生的账户减少并且已经增加了交易信息，
                            if (tf2 == true) {
                                //说明医生的账户金额已经减少了，那么就该增加每一位患者的金额
                                tf3 = accountMapper.updateMoneyByLoginId(PatientLoginId, money) > 0 ? true : false;
                                //增加了患者账户余额后，再增加相应的交易信息

                                Transaction transaction1 = new Transaction();
                                //付款登录用户id
                                transaction1.setLoginId(loginId);
                                //收款用户id
                                transaction1.setCollectionLoginid(PatientLoginId);
                                //交易类型
                                transaction1.setTransactionStatuetype(1);
                                //交易时间
                                transaction1.setTransactionTime(new Date());
                                //交易核桃币数量
                                transaction1.setTransactionVirtualmoney(money);
                                //交易场景
                                transaction1.setTransactionType("参加患教活动赚取");
                                //交易状态（1成功  2 失败 3待支付）
                                transaction1.setTransactionState(1);

                                //患者账户金额结算成功，添加交易信息
                                if (tf3 == true) {
                                    tf4 = transactionMapper.insertSelective(transaction1) > 0 ? true : false;
                                    if (tf4 == true) {
                                        continue;
                                    } else {
                                        return false;
                                    }
                                } else {
                                    return false;
                                }
                            } else {
                                return false;
                            }
                        }

                        return true;
                    }
                    return false;
                }
                return false;
            }
            //说明没人观看，不用分钱和添加交易信息，直接返回成功
            return true;
        }

        return false;

    }

    @Override
    public Integer selectDoctorIdByLoginId(Integer loginId) {

        return loginMapper.selectDoctorIdByLoginId(loginId);
    }
}
